// 模拟数据
import dayjs from 'dayjs';
export const mockSamples = [
    {
        id: "b1e680aa067e4eadac86beb65d4af79e",
        code: 10001,
        name: '样地A',
        lng: 116.397128,
        lat: 39.916527,
        delFlag: null,
        updatedAt: dayjs().subtract(1, 'day').unix(), // 1天前 - 秒级时间戳
    },
    {
        id: "c2f791bb178f5fbebf97cfc76e5bg80f",
        code: 10002,
        name: '样地B',
        lng: 116.407394,
        lat: 39.904211,
        delFlag: null,
        updatedAt: dayjs().subtract(2, 'day').unix(), // 2天前 - 秒级时间戳
    },
    {
        id: "d3g802cc289g6gcfcg08dfd87f6ch91g",
        code: 10003,
        name: '样地C',
        lng: 116.387112,
        lat: 39.924563,
        delFlag: null,
        updatedAt: dayjs().subtract(3, 'day').unix(), // 3天前 - 秒级时间戳
    },
];

export const mockDevices = [
    {
        id: "049602f52c8a4e609c02f148b0048a9e",
        sample: 68,
        waybatch: 0,
        waycode: 0,
        type: 2,
        batch: 250908,
        code: 4,
        voltage: 718,
        mesa: 0,
        angle: 87,
        tmp: 0,
        rssi: 22,
        seconds: 1757555038,
        lng: 114.12,
        lat: 24.432,
        hardware: "SJ_4G_MB_R1D\u0000\u0000\u0000",
        software: "BleGw-RV5.2.2\u0000\u0000",
        imsi: "460041640429875",
        param: "{\n   \"scanTime\": 30,\n   \"netOverTime\": 20000,\n   \"waywakeup\": 86400,\n   \"nextwakeup\": 1757586653,\n   \"dbpower\": 1\n}",
        updatedAt: 1757555038,
        delFlag: false
    },
    {
        id: "159703g63d9b5f710d13g259c1159b0f",
        sample: 68,
        waybatch: 0,
        waycode: 0,
        type: 0,
        batch: 250908,
        code: 5,
        voltage: 695,
        mesa: 12,
        angle: 45,
        tmp: 2350, // 23.5°C * 100
        rssi: -65,
        seconds: dayjs().subtract(1, 'hour').unix(),
        lng: 114.15,
        lat: 24.435,
        hardware: "SJ_4G_MB_R1B\u0000\u0000\u0000",
        software: "BleGw-RV5.1.8\u0000\u0000",
        imsi: "460041640429876",
        param: "{\n   \"scanTime\": 60,\n   \"netOverTime\": 15000,\n   \"waywakeup\": 43200,\n   \"nextwakeup\": 1757592000,\n   \"dbpower\": 2\n}",
        updatedAt: dayjs().subtract(1, 'hour').unix(),
        delFlag: false
    },
    {
        id: "260804h74e0c6g821e24h360d2260c1g",
        sample: 69,
        waybatch: 0,
        waycode: 0,
        type: 1,
        batch: 250909,
        code: 6,
        voltage: 702,
        mesa: 8,
        angle: 92,
        tmp: 2420, // 24.2°C * 100
        rssi: -72,
        seconds: dayjs().subtract(2, 'hour').unix(),
        lng: 114.18,
        lat: 24.438,
        hardware: "SJ_4G_MB_R1C\u0000\u0000\u0000",
        software: "BleGw-RV5.3.0\u0000\u0000",
        imsi: "460041640429877",
        param: "{\n   \"scanTime\": 45,\n   \"netOverTime\": 25000,\n   \"waywakeup\": 21600,\n   \"nextwakeup\": 1757598400,\n   \"dbpower\": 3\n}",
        updatedAt: dayjs().subtract(2, 'hour').unix(),
        delFlag: false
    },
    // 为当前实际设备添加完整记录 (sample: 0, batch: 240615, code: 1346)
    {
        id: "09bfef5da9f441cd9293b2837b2f5444",
        sample: 0,
        waybatch: 0,
        waycode: 0,
        type: 0,
        batch: 240615,
        code: 1346,
        voltage: 375,
        mesa: 28.5,
        angle: 62,
        tmp: 2480,
        rssi: -58,
        seconds: dayjs().subtract(30, 'minute').unix(),
        lng: 114.18,
        lat: 24.445,
        hardware: "SJ_4G_MB_R1D\u0000\u0000\u0000",
        software: "BleGw-RV5.2.2\u0000\u0000",
        imsi: "460041640429875",
        param: "{\n   \"scanTime\": 30,\n   \"netOverTime\": 20000,\n   \"waywakeup\": 86400,\n   \"nextwakeup\": 1757586653,\n   \"dbpower\": 1\n}",
        updatedAt: dayjs().subtract(30, 'minute').unix(),
        delFlag: false
    },
];

export const mockDeviceData = [
    {
        id: "device_data_1",
        sample: 68,
        waybatch: 0,
        waycode: 0,
        batch: 250908,
        code: 4,
        voltage: 370, // 3.7V * 100
        mesa: 25.6,
        angle: 45,
        tmp: 2300, // 23.0°C * 100
        rssi: -65,
        lng: 114.12,
        lat: 24.432,
        updatedAt: dayjs().unix(), // 今天的数据 - 秒级时间戳
    },
    {
        id: "device_data_2",
        sample: 68,
        waybatch: 0,
        waycode: 0,
        batch: 250908,
        code: 4,
        voltage: 360, // 3.6V * 100
        mesa: 26.2,
        angle: 50,
        tmp: 2400, // 24.0°C * 100
        rssi: -68,
        lng: 114.12,
        lat: 24.432,
        updatedAt: dayjs().subtract(2, 'hour').unix(), // 今天2小时前 - 秒级时间戳
    },
    {
        id: "device_data_3",
        sample: 68,
        waybatch: 0,
        waycode: 0,
        batch: 250908,
        code: 5,
        voltage: 695,
        mesa: 12,
        angle: 87,
        tmp: 2350,
        rssi: 22,
        lng: 114.15,
        lat: 24.435,
        updatedAt: dayjs().subtract(1, 'hour').unix(),
    },
    // 为当前设备添加匹配数据 (sample: 0, batch: 240615, code: 1346)
    {
        id: "device_data_4",
        sample: 0,
        waybatch: 0,
        waycode: 0,
        batch: 240615,
        code: 1346,
        voltage: 375,
        mesa: 28.5,
        angle: 62,
        tmp: 2480,
        rssi: -58,
        lng: 114.18,
        lat: 24.445,
        updatedAt: dayjs().subtract(30, 'minute').unix(),
    },
    {
        id: "device_data_5",
        sample: 0,
        waybatch: 0,
        waycode: 0,
        batch: 240615,
        code: 1346,
        voltage: 380,
        mesa: 29.1,
        angle: 58,
        tmp: 2520,
        rssi: -62,
        lng: 114.18,
        lat: 24.445,
        updatedAt: dayjs().subtract(1, 'hour').unix(),
    },
    // 添加更多设备数据以测试分页和统计
    {
        id: "device_data_6",
        sample: 0,
        waybatch: 0,
        waycode: 0,
        batch: 250908,
        code: 4,
        voltage: 365,
        mesa: 24.8,
        angle: 42,
        tmp: 2280,
        rssi: -70,
        lng: 114.10,
        lat: 24.430,
        updatedAt: dayjs().subtract(3, 'hour').unix(),
    },
    {
        id: "device_data_7",
        sample: 0,
        waybatch: 0,
        waycode: 0,
        batch: 250908,
        code: 4,
        voltage: 372,
        mesa: 25.2,
        angle: 47,
        tmp: 2320,
        rssi: -66,
        lng: 114.11,
        lat: 24.431,
        updatedAt: dayjs().subtract(4, 'hour').unix(),
    },
    {
        id: "device_data_8",
        sample: 0,
        waybatch: 0,
        waycode: 0,
        batch: 250908,
        code: 5,
        voltage: 355,
        mesa: 23.5,
        angle: 38,
        tmp: 2200,
        rssi: -72,
        lng: 114.09,
        lat: 24.428,
        updatedAt: dayjs().subtract(5, 'hour').unix(),
    },
    {
        id: "device_data_9",
        sample: 0,
        waybatch: 1,
        waycode: 1,
        batch: 250909,
        code: 6,
        voltage: 368,
        mesa: 26.8,
        angle: 55,
        tmp: 2450,
        rssi: -64,
        lng: 114.13,
        lat: 24.434,
        updatedAt: dayjs().subtract(6, 'hour').unix(),
    },
    {
        id: "device_data_10",
        sample: 1,
        waybatch: 1,
        waycode: 1,
        batch: 250909,
        code: 7,
        voltage: 374,
        mesa: 27.2,
        angle: 52,
        tmp: 2380,
        rssi: -61,
        lng: 114.14,
        lat: 24.436,
        updatedAt: dayjs().subtract(7, 'hour').unix(),
    },
    {
        id: "device_data_11",
        sample: 1,
        waybatch: 1,
        waycode: 1,
        batch: 250909,
        code: 8,
        voltage: 360,
        mesa: 24.1,
        angle: 43,
        tmp: 2310,
        rssi: -69,
        lng: 114.12,
        lat: 24.433,
        updatedAt: dayjs().subtract(8, 'hour').unix(),
    },
    {
        id: "device_data_12",
        sample: 1,
        waybatch: 1,
        waycode: 1,
        batch: 250910,
        code: 9,
        voltage: 378,
        mesa: 28.9,
        angle: 60,
        tmp: 2520,
        rssi: -59,
        lng: 114.16,
        lat: 24.438,
        updatedAt: dayjs().subtract(9, 'hour').unix(),
    },
];

export const mockTempHumidityData = [
    {
        id: "temp_humidity_1",
        sample: 68,
        batch: 250908,
        code: 4,
        temp: 2350, // 23.5°C * 100
        humi: 6520, // 65.2% * 100
        simpletime: dayjs().subtract(30, 'minute').unix(), // 30分钟前 - 秒级时间戳
    },
    {
        id: "temp_humidity_2",
        sample: 68,
        batch: 250908,
        code: 4,
        temp: 2410, // 24.1°C * 100
        humi: 6380, // 63.8% * 100
        simpletime: dayjs().subtract(1, 'hour').unix(), // 1小时前 - 秒级时间戳
    },
    {
        id: "temp_humidity_3",
        sample: 68,
        batch: 250908,
        code: 5,
        temp: 2380, // 23.8°C * 100
        humi: 6750, // 67.5% * 100
        simpletime: dayjs().subtract(45, 'minute').unix(), // 45分钟前 - 秒级时间戳
    },
    // 为当前设备添加匹配数据 (sample: 0, batch: 240615, code: 1346)
    {
        id: "temp_humidity_4",
        sample: 0,
        batch: 240615,
        code: 1346,
        temp: 2520, // 25.2°C * 100
        humi: 7230, // 72.3% * 100
        simpletime: dayjs().subtract(20, 'minute').unix(), // 20分钟前 - 秒级时间戳
    },
    {
        id: "temp_humidity_5",
        sample: 0,
        batch: 240615,
        code: 1346,
        temp: 2490, // 24.9°C * 100
        humi: 7180, // 71.8% * 100
        simpletime: dayjs().subtract(50, 'minute').unix(), // 50分钟前 - 秒级时间戳
    },
    // 添加更多数据以测试分页和统计
    {
        id: "temp_humidity_6",
        sample: 0,
        batch: 250908,
        code: 4,
        temp: 2600, // 26.0°C * 100
        humi: 5500, // 55.0% * 100
        simpletime: dayjs().subtract(2, 'hour').unix(),
    },
    {
        id: "temp_humidity_7",
        sample: 0,
        batch: 250908,
        code: 4,
        temp: 2550, // 25.5°C * 100
        humi: 5800, // 58.0% * 100
        simpletime: dayjs().subtract(3, 'hour').unix(),
    },
    {
        id: "temp_humidity_8",
        sample: 0,
        batch: 250908,
        code: 4,
        temp: 2480, // 24.8°C * 100
        humi: 6200, // 62.0% * 100
        simpletime: dayjs().subtract(4, 'hour').unix(),
    },
];

export const mockDeviceParams = [
    {
        id: "device_param_1",
        sample: 68,
        batch: 250908,
        code: 4,
        paramName: '采样频率',
        paramValue: '10',
        unit: '分钟',
        updatedAt: dayjs().subtract(3, 'hour').unix(), // 3小时前 - 秒级时间戳
    },
    {
        id: "device_param_2",
        sample: 68,
        batch: 250908,
        code: 4,
        paramName: '传输功率',
        paramValue: '20',
        unit: 'dBm',
        updatedAt: dayjs().subtract(3, 'hour').unix(), // 3小时前 - 秒级时间戳
    },
    {
        id: "device_param_3",
        sample: 68,
        batch: 250908,
        code: 5,
        paramName: '采样频率',
        paramValue: '15',
        unit: '分钟',
        updatedAt: dayjs().subtract(2, 'hour').unix(), // 2小时前 - 秒级时间戳
    },
    // 为当前设备添加匹配数据 (sample: 0, batch: 240615, code: 1346)
    {
        id: "device_param_4",
        sample: 0,
        batch: 240615,
        code: 1346,
        paramName: '采样频率',
        paramValue: '5',
        unit: '分钟',
        updatedAt: dayjs().subtract(1, 'hour').unix(), // 1小时前 - 秒级时间戳
    },
    {
        id: "device_param_5",
        sample: 0,
        batch: 240615,
        code: 1346,
        paramName: '传输功率',
        paramValue: '18',
        unit: 'dBm',
        updatedAt: dayjs().subtract(1, 'hour').unix(), // 1小时前 - 秒级时间戳
    },
    {
        id: "device_param_6",
        sample: 0,
        batch: 240615,
        code: 1346,
        paramName: '休眠时间',
        paramValue: '120',
        unit: '秒',
        updatedAt: dayjs().subtract(1, 'hour').unix(), // 1小时前 - 秒级时间戳
    },
];

// 模拟API响应格式
export const createMockResponse = (data, total = null) => {
    const totalCount = total || data.length;
    return Promise.resolve({
        code: 0,
        msg: '操作成功',
        data: {
            list: data,
            total: totalCount,  // 保持兼容性
            totalCount: totalCount,  // API实际使用的字段
            pageNo: 1,
            pageSize: 10,
            totalPage: Math.ceil(totalCount / 10),
            maxPage: Math.ceil(totalCount / 10),
            minPage: 1,
        },
    });
};

// 样地搜索过滤函数
export const filterSamples = (samples, searchParams) => {
    return samples.filter(sample => {
        let match = true;

        // 按样地编号搜索（精确匹配，确保整数比较）
        if (searchParams.code && searchParams.code !== 0) {
            const searchCode = parseInt(searchParams.code);
            const sampleCode = parseInt(sample.code);
            match = match && sampleCode === searchCode;
        }

        // 按样地名称搜索（模糊匹配）
        if (searchParams.name && searchParams.name.trim()) {
            match = match && sample.name &&
                sample.name.toLowerCase().includes(searchParams.name.toLowerCase());
        }

        return match;
    });
};

// 设备搜索过滤函数
export const filterDevices = (devices, searchParams) => {
    return devices.filter(device => {
        let match = true;

        // 按样地编号搜索（精确匹配）
        if (searchParams.sample && searchParams.sample !== 0) {
            const searchSample = parseInt(searchParams.sample);
            const deviceSample = parseInt(device.sample);
            match = match && deviceSample === searchSample;
        }

        // 按设备类型搜索（精确匹配）
        if (searchParams.type !== undefined && searchParams.type !== null) {
            match = match && device.type === searchParams.type;
        }

        // 按设备批次号搜索（精确匹配）
        if (searchParams.batch && searchParams.batch !== 0) {
            const searchBatch = parseInt(searchParams.batch);
            const deviceBatch = parseInt(device.batch);
            match = match && deviceBatch === searchBatch;
        }

        // 按设备编号搜索（精确匹配）
        if (searchParams.code && searchParams.code !== 0) {
            const searchCode = parseInt(searchParams.code);
            const deviceCode = parseInt(device.code);
            match = match && deviceCode === searchCode;
        }

        // 注意：实际API中设备数据没有MAC地址字段

        return match;
    });
};

// 设备数据搜索过滤函数
export const filterDeviceData = (deviceData, searchParams) => {
    // 如果没有搜索参数，返回所有数据
    if (!searchParams || Object.keys(searchParams).length === 0) {
        return deviceData;
    }

    const filtered = deviceData.filter(data => {
        let match = true;

        // 按样地编号搜索 - 包括0值
        if (searchParams.sample !== undefined && searchParams.sample !== null && searchParams.sample !== '') {
            match = match && parseInt(data.sample) === parseInt(searchParams.sample);
        }

        // 按设备批次号搜索 - 包括0值
        if (searchParams.batch !== undefined && searchParams.batch !== null && searchParams.batch !== '') {
            match = match && parseInt(data.batch) === parseInt(searchParams.batch);
        }

        // 按设备编号搜索 - 包括0值
        if (searchParams.code !== undefined && searchParams.code !== null && searchParams.code !== '') {
            match = match && parseInt(data.code) === parseInt(searchParams.code);
        }

        // 按网关批次搜索 - 包括0值
        if (searchParams.waybatch !== undefined && searchParams.waybatch !== null && searchParams.waybatch !== '') {
            match = match && parseInt(data.waybatch) === parseInt(searchParams.waybatch);
        }

        // 按网关编号搜索 - 包括0值
        if (searchParams.waycode !== undefined && searchParams.waycode !== null && searchParams.waycode !== '') {
            match = match && parseInt(data.waycode) === parseInt(searchParams.waycode);
        }

        // 时间范围过滤
        if (searchParams.startTime && searchParams.endTime) {
            // updatedAt是秒级时间戳，需要转换为毫秒
            const dataTime = new Date((data.updatedAt || data.simpletime) * 1000);
            const startTime = new Date(searchParams.startTime);
            const endTime = new Date(searchParams.endTime);
            match = match && dataTime >= startTime && dataTime <= endTime;
        }

        return match;
    });

    return filtered;
};

// 温湿度数据搜索过滤函数
export const filterTempHumidityData = (tempData, searchParams) => {
    // 如果没有搜索参数，返回所有数据
    if (!searchParams || Object.keys(searchParams).length === 0) {
        return tempData;
    }

    const filtered = tempData.filter(data => {
        let match = true;

        // 按样地编号搜索 - 包括0值
        if (searchParams.sample !== undefined && searchParams.sample !== null && searchParams.sample !== '') {
            match = match && parseInt(data.sample) === parseInt(searchParams.sample);
        }

        // 按设备批次号搜索 - 包括0值
        if (searchParams.batch !== undefined && searchParams.batch !== null && searchParams.batch !== '') {
            match = match && parseInt(data.batch) === parseInt(searchParams.batch);
        }

        // 按设备编号搜索 - 包括0值
        if (searchParams.code !== undefined && searchParams.code !== null && searchParams.code !== '') {
            match = match && parseInt(data.code) === parseInt(searchParams.code);
        }

        // 时间范围过滤
        if (searchParams.startTime && searchParams.endTime) {
            // simpletime是秒级时间戳，需要转换为毫秒
            const dataTime = new Date(data.simpletime * 1000);
            const startTime = new Date(searchParams.startTime);
            const endTime = new Date(searchParams.endTime);
            match = match && dataTime >= startTime && dataTime <= endTime;
        }

        return match;
    });

    return filtered;
};

// 设备参数搜索过滤函数
export const filterDeviceParams = (paramsData, searchParams) => {
    // 如果没有搜索参数，返回所有数据
    if (!searchParams || Object.keys(searchParams).length === 0) {
        return paramsData;
    }

    const filtered = paramsData.filter(data => {
        let match = true;

        // 按样地编号搜索 - 包括0值
        if (searchParams.sample !== undefined && searchParams.sample !== null && searchParams.sample !== '') {
            match = match && parseInt(data.sample) === parseInt(searchParams.sample);
        }

        // 按设备批次号搜索 - 包括0值
        if (searchParams.batch !== undefined && searchParams.batch !== null && searchParams.batch !== '') {
            match = match && parseInt(data.batch) === parseInt(searchParams.batch);
        }

        // 按设备编号搜索 - 包括0值
        if (searchParams.code !== undefined && searchParams.code !== null && searchParams.code !== '') {
            match = match && parseInt(data.code) === parseInt(searchParams.code);
        }

        // 按IMSI搜索
        if (searchParams.imsi && searchParams.imsi !== '') {
            match = match && data.imsi && data.imsi.includes(searchParams.imsi);
        }

        // 时间范围过滤
        if (searchParams.startTime && searchParams.endTime) {
            // updatedAt是秒级时间戳，需要转换为毫秒
            const dataTime = new Date(data.updatedAt * 1000);
            const startTime = new Date(searchParams.startTime);
            const endTime = new Date(searchParams.endTime);
            match = match && dataTime >= startTime && dataTime <= endTime;
        }

        return match;
    });

    return filtered;
};
